package pl.edu.agh.jemo.gui.panels.mutation;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import pl.edu.agh.jemo.evolution.operator.mutation.impl.Hillclimbing;

/**
 * Configuration panel allowing to adjust hillclimbing mutation parameters.
 * As an extension of JPanel, it can be displayed by main window, whenever it is needed.
 * It works on existing mutation instance, using reference passed in constructor.
 * 
 * @author Marcin Zbijowski
 *
 */
public class HillClimbingConfigPanel extends JPanel {
	/**
	 * As JPanel is implementation of Serializable interface, it requires this field to be set.
	 */
	private static final long serialVersionUID = 8208507540996128128L;

	/**
	 * Reference to hillclimbing instance configured by panel.
	 */
	protected final Hillclimbing mutation;
	
	/**
	 * Text field allowing to edit hillclimbing multiplier parameter.
	 */
	protected JTextField multiplierField;

	/**
	 * Creates Panel instance associated with given mutation reference.
	 * Initializes all graphic components, so after constructor no further initialization is needed.
	 * 
	 * @param hillClimbing reference to hillclimbing mutation, that needs configuration. 
	 */
	public HillClimbingConfigPanel(Hillclimbing hillClimbing) {
		super();
		mutation = hillClimbing;
		
		setLayout(null);
		
		JLabel multiplierLabel = new JLabel("Hillclimbing multiplier:");
		multiplierLabel.setBounds(10, 10, 150, 15);
		add(multiplierLabel);
		
		multiplierField = new JTextField(String.valueOf(mutation.getMultiplier()));
		multiplierField.setBounds(10, 27, 150, 22);
		add(multiplierField);
		
		multiplierField.addActionListener(new ActionListener() {	
			@Override
			public void actionPerformed(ActionEvent event) {
				try {
					mutation.setMultiplier(Double.parseDouble(multiplierField.getText()));
				} catch (NumberFormatException e) {
					JOptionPane.showMessageDialog(null, "Wrong double value");
				}
			}
		});
	}

}
